﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomationHost.ViewModels;

namespace AutomationHost.Areas.Plan.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["HisoftSmartEntities"].ConnectionString;
            
        public ActionResult Index()
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                PlanIndexPage model = new PlanIndexPage();
                model.ProductTree = new Models.ProductTree() {Products = new List<Product>()};
                foreach (var p in db.Products.ToList())
                {
                    p.FeaturesModel = new List<Feature>();
                    foreach (var f in p.Features.ToList())
                    {
                        f.TestCasesModel = new List<TestCase>();
                        foreach (var tc in f.TestCases.ToList())
                        {
                            f.TestCasesModel.Add(tc);
                        }

                        p.FeaturesModel.Add(f);
                    }

                    model.ProductTree.Products.Add(p);                    
                }

                if (!string.IsNullOrEmpty(Request["caseid"]))
                {
                    int id = int.Parse(Request["caseid"]);
                    model.CurrentTestCases = db.TestCases.Where(c => c.ID == id).ToList();
                }
                else if (!string.IsNullOrEmpty(Request["featureid"]))
                {
                    int id = int.Parse(Request["featureid"]);
                    model.CurrentTestCases = db.TestCases.Where(c => c.FeatureID == id).ToList();
                }
                else if (!string.IsNullOrEmpty(Request["productid"]))
                {
                    int id = int.Parse(Request["productid"]);
                    var cases = from p in db.Products
                                from f in db.Features
                                from t in db.TestCases
                                where p.ID == id && f.ProductID == p.ID && t.FeatureID == f.ID
                                select t;
                    model.CurrentTestCases = cases.ToList();
                }
                else
                {
                    model.CurrentTestCases = new List<TestCase>();
                }

                return View(model);
            }
        }

        public ActionResult List()
        {
            using (HisoftSmartEntities db = new HisoftSmartEntities(conn))
            {
                PlanListPage model = new PlanListPage();
                model.TestPlans = db.TestPlans.ToList();

                if (!string.IsNullOrEmpty(Request["planid"]))
                {
                    int id = int.Parse(Request["planid"]);
                    string caseIDs = db.TestPlans.First(t => t.ID == id).CaseIDs;
                    List<TestCase> cases = new List<TestCase>();
                    foreach (var caseId in caseIDs.Split(','))
                    {
                        if (!string.IsNullOrEmpty(caseId))
                        {
                            int cid = int.Parse(caseId);
                            cases.Add(db.TestCases.First(c => c.ID == cid));
                        }
                    }

                    model.CurrentTestCases = cases;
                }
                else
                {
                    model.CurrentTestCases = new List<TestCase>();
                }

                return View(model);
            }
        }
    }
}
